﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace WPay.Infrastructure.Unity.Wxpay
{
    class RSAHelper
    {
        /// 最终提交请求时，需对敏感信息加密，如身份证、银行卡号。
        /// 加密算法是RSA，使用从接口下载到的公钥进行加密，非后台下载到的私钥。
        /// 感谢ISV(https://github.com/ndma-isv)提供示例
        /// 
        /// 

        /// 要加密的明文
        ///  -----BEGIN CERTIFICATE----- 开头的string，转为bytes 
        /// 
        public static string RSAEncrypt(string text)
        {
            string path = "";
            string SSLCERT_PASSWORD = "";
            using (var x509 = new X509Certificate2(path, SSLCERT_PASSWORD, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet))
            {
                using (var rsa = (RSACryptoServiceProvider)x509.PublicKey.Key)
                {
                    var buff = rsa.Encrypt(Encoding.UTF8.GetBytes(text), true);

                    return Convert.ToBase64String(buff);
                }
            }
        }
    }
}
